package com.cc.cbdb.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cc.cbdb.R;
import com.cc.cbdb.activity.X5WebViewActivity;
import com.cc.cbdb.adapter.base.BaseCommAdapter;
import com.cc.cbdb.adapter.base.ViewHolder;
import com.cc.cbdb.common.constants.Constant;
import com.cc.cbdb.common.utils.StringUtil;
import com.cc.cbdb.entity.SelectedCustom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2018/4/10.
 */

public class SelectedDataListAdapter extends BaseCommAdapter<SelectedCustom> {

    private static final String TAG = "SelectedDataListAdapter";
    private final int itemStyle;

    private List<SelectedCustom> mList;
    private HashMap<Integer,Boolean> checkStatus;
    private CheckedListener listener;
    private List<CheckBox> mCbList;
    private Activity mActivity;

    public void setListener(CheckedListener mListener) {
        this.listener = mListener;
    }

    public SelectedDataListAdapter(Activity activity,int itemStyle,List<SelectedCustom> datas, HashMap<Integer,Boolean> checkStatus) {
        super(datas);
        this.mActivity = activity;
        this.itemStyle = itemStyle;
        this.mList = datas;
        this.checkStatus = checkStatus;
        mCbList = new ArrayList<>();
    }

    @Override
    protected void setUI(ViewHolder holder, int position, Context context) {
        final SelectedCustom item = getItem(position);
        final boolean isChecked = checkStatus.get(item.getId());
//        Log.d(TAG,"id:"+item.getId()+" name:"+item.getName()+" checkstatus:"+isChecked);
        LinearLayout ll = holder.getItemView(R.id.ll);
        final CheckBox cb = holder.getItemView(R.id.cb);
        TextView name = holder.getItemView(R.id.name);
        name.setText(item.getName());
        mCbList.add(cb);
        if (itemStyle == 0){
            TextView tv_desc = holder.getItemView(R.id.tv_desc);
            tv_desc.setText(item.getDesc());
        }else if (itemStyle == 1) {
            cb.setVisibility(View.VISIBLE);
            TextView nameChn = holder.getItemView(R.id.name_chn);
            TextView dyTitle = holder.getItemView(R.id.dynasty_title);
            TextView dynasty = holder.getItemView(R.id.dynasty);
            TextView alnTitle =holder.getItemView(R.id.alert_name_title);
            TextView alertName = holder.getItemView(R.id.alert_name);
            TextView more = holder.getItemView(R.id.more);

            name.setText(item.getName());
            nameChn.setText(item.getNameChn());
            dyTitle.setText("朝代：");
            dynasty.setText(item.getDyChn());
            alnTitle.setText("别名：");
            alertName.setText(StringUtil.isEmpty(item.getAltName()) ? "无" : item.getAltName());

            more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mActivity, X5WebViewActivity.class);
                    StringBuilder sb = new StringBuilder(Constant.DOMAIN_PROJECT);
                    sb.append(Constant.HTML.HTML_QPOSTOFFTAB);
                    sb.append("#");
                    sb.append(item.getId());
                    intent.putExtra("url",sb.toString());
                    intent.putExtra("title",item.getNameChn()+"的仕途");
                    mActivity.startActivity(intent);
                }
            });
        }
        cb.setChecked(isChecked);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean nowStatus = !cb.isChecked();
                checkStatus.put(item.getId(),nowStatus);
                cb.setChecked(nowStatus);
                listener.statisticChecked(nowStatus);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        int layoutId = 0;
        if (itemStyle == 0){
            layoutId = R.layout.item_selected_content;
        }else if (itemStyle == 1){
            layoutId = R.layout.item_search_content;
        }
        return layoutId;
    }

    public interface CheckedListener{
        void statisticChecked(boolean isChecked);
    }

    public void checkAll(){
        int size = mList.size();
        int cbSize = mCbList.size();

        for (int i = 0; i < size; i++) {
            checkStatus.put(mList.get(i).getId(),true);
            if (i < cbSize) {
                mCbList.get(i).setChecked(true);
            }
        }
    }

    public void disCheckAll(){
        int size = mList.size();
        int cbSize = mCbList.size();
        for (int i = 0; i < size; i++) {
            checkStatus.put(mList.get(i).getId(),false);
            if (i < cbSize){
                mCbList.get(i).setChecked(false);
            }
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        mCbList.clear();
    }
}
